<template>
    <view class="jnpf-wrap jnpf-wrap-form" v-if="!loading">
        <u-form :model="dataForm" :rules="rules" ref="dataForm" :errorType="['toast']"
                label-position="left" label-align="right" :label-width="labelwidth" class="jnpf-form">
			<u-form-item  
 label="特种证书名称"  prop="specialworkname" >
				<u-input v-model="dataForm.specialworkname"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="所在车间"  prop="workshop" >
				<u-input v-model="dataForm.workshop"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="证书持有人"  prop="specialpeoplename" >
				<u-input v-model="dataForm.specialpeoplename"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="所在班组"  prop="workteam" >
				<u-input v-model="dataForm.workteam"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="工种"  prop="worktype" >
				<u-input v-model="dataForm.worktype"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="专业工龄"  prop="specialworktime" >
				<u-input v-model="dataForm.specialworktime"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="证书编号"  prop="specialworknum" >
				<u-input v-model="dataForm.specialworknum"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="发证日期"  prop="specialissuedate" >
				<jnpf-date-time v-model="dataForm.specialissuedate"
 placeholder="请选择" type="date" :style='{"width":"100%"}'>
				</jnpf-date-time>
			</u-form-item>
			<u-form-item  
 label="发证机关"  prop="specialissuegroup" >
				<u-input v-model="dataForm.specialissuegroup"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="培训时间"  prop="studytime" >
				<jnpf-date-time v-model="dataForm.studytime"
 placeholder="请选择" type="date" :style='{"width":"100%"}'>
				</jnpf-date-time>
			</u-form-item>
			<u-form-item  
 label="一次复审"  prop="onetrial" >
				<jnpf-date-time v-model="dataForm.onetrial"
 placeholder="请选择" type="date" :style='{"width":"100%"}'>
				</jnpf-date-time>
			</u-form-item>
			<u-form-item  
 label="二次复审"  prop="twotrial" >
				<jnpf-date-time v-model="dataForm.twotrial"
 placeholder="请选择" type="date" :style='{"width":"100%"}'>
				</jnpf-date-time>
			</u-form-item>
			<u-form-item  
 label="有效期"  prop="validitytime" >
				<jnpf-date-time v-model="dataForm.validitytime"
 placeholder="请选择" type="date" :style='{"width":"100%"}'>
				</jnpf-date-time>
			</u-form-item>
			<u-form-item  
 label="延期时间"  prop="delaytime" >
				<jnpf-date-time v-model="dataForm.delaytime"
 placeholder="请选择" type="date" :style='{"width":"100%"}'>
				</jnpf-date-time>
			</u-form-item>
			<u-form-item  
 label="延长时间"  prop="extendtime" >
				<u-input v-model="dataForm.extendtime"
 placeholder="请输入" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			<u-form-item  
 label="特种证图"  prop="specialimage" >
				<jnpf-upload v-model="dataForm.specialimage"
 sizeUnit="MB"  :fileSize="5"  :limit="9"  :value="dataForm.specialimage">
				</jnpf-upload>
			</u-form-item>
			<view prop="activexfsltz">
			<u-tabs :is-scroll="false" :list="activexfsltzData" name="title" :current="activexfsltzCurrent" @change="activexfsltz"/>
			<view>
			<view v-if="0 == activexfsltzCurrent">
            <view class="jnpf-table" >
			<view class="jnpf-table-item" v-for="(item,i) in dataForm.mt_safe_accident_listList" :key="i">
			<view class="jnpf-table-item-title u-flex u-row-between">
			<view class="jnpf-table-item-title-action" v-if="dataForm.mt_safe_accident_listList.length>1" @click="delmt_safe_accident_listList(i)">删除</view>
			</view>
			<u-form-item 
 label="事故发生时间" >
				<jnpf-date-time v-model="dataForm.mt_safe_accident_listList[i].accidenttime"
 placeholder="请选择" type="date" :style='{"width":"100%"}'>
				</jnpf-date-time>
			</u-form-item>
			<u-form-item 
 label="事故内容" >
				<u-input v-model="dataForm.mt_safe_accident_listList[i].accidentcontent"
 placeholder="请输入" type="textarea" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			</view>
			<view class="jnpf-table-addBtn" @click="addmt_safe_accident_listList" >
			<u-icon name="plus" color="#2979ff"></u-icon>添加
			</view>
            </view>
			</view>
			<view v-if="1 == activexfsltzCurrent">
            <view class="jnpf-table" >
			<view class="jnpf-table-item" v-for="(item,i) in dataForm.mt_safe_violation_listList" :key="i">
			<view class="jnpf-table-item-title u-flex u-row-between">
			<text class="jnpf-table-item-title-num">设计子表({{i+1}})</text>
			<view class="jnpf-table-item-title-action" v-if="dataForm.mt_safe_violation_listList.length>1" @click="delmt_safe_violation_listList(i)">删除</view>
			</view>
			<u-form-item 
 label="违法违章时间" >
				<jnpf-date-time v-model="dataForm.mt_safe_violation_listList[i].violationtime"
 placeholder="请选择" type="date" :style='{"width":"100%"}'>
				</jnpf-date-time>
			</u-form-item>
			<u-form-item 
 label="违法违章内容" >
				<u-input v-model="dataForm.mt_safe_violation_listList[i].violationcontent"
 placeholder="请输入" type="textarea" :style='{"width":"100%"}'>
				</u-input>
			</u-form-item>
			</view>
			<view class="jnpf-table-addBtn" @click="addmt_safe_violation_listList" >
			<u-icon name="plus" color="#2979ff"></u-icon>添加
			</view>
            </view>
			</view>
			</view>
			</view>
        </u-form>
        <view class="buttom-actions" v-if="jurisdictionType != 'btn_detail'" >
            <u-button class="buttom-btn" @click="resetForm">取消</u-button>
            <u-button class="buttom-btn" type="primary" @click="submitForm" :loading="btnLoading">保存</u-button>
        </view>
    </view>
</template>

<script>
    import {
        getDictionaryDataSelector,
		getDataInterfaceRes
    } from '@/api/common'
    import request from '@/utils/request'
    export default {
        data(){
            return{
                btnLoading: false,
				loading: false,
                text: '提示：测试文本',
				dataForm:{
					id:"",
					specialworkname : '',
					workshop : '',
					specialpeoplename : '',
					workteam : '',
					worktype : '',
					specialworktime : '',
					specialworknum : '',
					specialissuedate : '',
					specialissuegroup : '',
					studytime : '',
					onetrial : '',
					twotrial : '',
					validitytime : '',
					delaytime : '',
					extendtime : '',
					specialimage : [],
					mt_safe_accident_listList:[],
					mt_safe_violation_listList:[],
				},
				rules:{
				},
				activexfsltzCurrent:1,
				activexfsltzData:[
					{
						title: "事故责任记录"
					},
					{
						title: "违法违章记录"
					},
				],
                labelwidth:100*1.5,
                menuId:'',
				jurisdictionType:'',
                ruleList:{
                },
            }
        },
        onLoad(option) {
			this.jurisdictionType = option.jurisdictionType
        	this.menuId=option.menuId
            this.dataForm.id = option.id || ''
			let _title = ""
			if(option.jurisdictionType =='btn_edit'){
				_title = "编辑"
			}
			if(option.jurisdictionType =='btn_detail'){
				_title = "详情"
			}
			if(option.jurisdictionType =='btn_add'){
				_title = "新增"
			}
            uni.setNavigationBarTitle({
                title: _title
            })
            this.selfInit()
            this.dataAll()
            this.initData()
        },
        onReady() {
            this.$refs.dataForm.setRules(this.rules);
        },
        watch:{
        	dataForm: {
				handler(val, oldVal) {
					this.mt_safe_accident_list()
					this.mt_safe_violation_list()
				},
				deep: true
			}
        },
        methods:{
        	checkChildRule() {
				let title = [];
				for (let k in this.ruleList) {
					let num = k.split("-");
					let childKey = num[0];
					num.forEach((model, i) => {
						if (i == 1) {
							let childData = this.dataForm[childKey]
							childData.forEach((child, i) => {
								if (child[model] instanceof Array) {
									if (child[model].length == 0) {
										title.push(this.ruleList[k])
									}
								} else {
									if (!child[model]) {
										title.push(this.ruleList[k])
									}
								}
							})
						}
					})
				}
				if (title.length > 0) {
					return title[0]
				}
			},
			resetForm(){
				uni.navigateBack()
			},
			dataAll(){
			},
			mt_safe_accident_list(){
				let table = this.dataForm.mt_safe_accident_listList
				let summaryField =[]
				let summaryFieldName ={}
				let data ={}
				for (let i in summaryField) {
					let map = {}
					let val = 0
					for (let j = 0; j < table.length; j++) {
						let summary = table[j][summaryField[i]];
						if (summary) {
							let data = isNaN(summary) ? 0 :	Number(summary)
							val += data
						}
					}
					map.id = summaryField[i];
					map.name = summaryFieldName[summaryField[i]];
					map.val = val;
					data[summaryField[i]]=map;
				}
				return data;
			},
			mt_safe_violation_list(){
				let table = this.dataForm.mt_safe_violation_listList
				let summaryField =[]
				let summaryFieldName ={}
				let data ={}
				for (let i in summaryField) {
					let map = {}
					let val = 0
					for (let j = 0; j < table.length; j++) {
						let summary = table[j][summaryField[i]];
						if (summary) {
							let data = isNaN(summary) ? 0 :	Number(summary)
							val += data
						}
					}
					map.id = summaryField[i];
					map.name = summaryFieldName[summaryField[i]];
					map.val = val;
					data[summaryField[i]]=map;
				}
				return data;
			},
			activexfsltz(index) {
				this.activexfsltzCurrent = index;
			},
            initData(){
				this.$nextTick(function(){
					if (this.dataForm.id) {
						this.loading = true
						request({
							url: '/api/example/Mt_safe_special_work/'+this.dataForm.id,
							method: 'get',
						}).then(res => {
							this.dataInfo(res.data)
							this.loading = false
						})
					}
				})
            },
            submitForm(){
                var _data = this.dataList()
                this.$refs.dataForm.validate(valid => {
                    if (!valid) return
                    if (!!this.checkChildRule()) return
                    this.$u.toast(
						this.checkChildRule()
					)
                    this.btnLoading = true
                    if (this.dataForm.id) {
                        request({
                            url: '/api/example/Mt_safe_special_work/' + this.dataForm.id,
                            method: 'put',
                            data: _data,
                        }).then(res => {
                            uni.showToast({
                                title: res.msg,
                                complete: () => {
                                    setTimeout(() => {
                                        this.btnLoading = false
                                        uni.$emit('refresh')
                                        uni.navigateBack()
                                    }, 1500)
                                }
                            })
                        }).catch(() => {
							this.btnLoading = false
						})
                    }else {
                        request({
                            url: '/api/example/Mt_safe_special_work/',
                            method: 'post',
                            data: _data,
                        }).then(res => {
                            uni.showToast({
                                title: res.msg,
                                complete: () => {
                                    setTimeout(() => {
                                        this.btnLoading = false
                                        uni.$emit('refresh')
                                        uni.navigateBack()
                                    }, 1500)
                                }
                            })
                        }).catch(() => {
							this.btnLoading = false
						})
                    }
                });
            },
            selfInit() {
				this.addmt_safe_accident_listList()
				this.addmt_safe_violation_listList()
                this.$store.commit('base/UPDATE_RELATION_DATA', {})
            },
            addmt_safe_accident_listList(){
                let item = {
					accidenttime:undefined,
					accidentcontent:undefined,
                }
				this.dataForm.mt_safe_accident_listList.push(item)
            },
            delmt_safe_accident_listList(index) {
                this.dataForm.mt_safe_accident_listList.splice(index, 1);
            },
            addmt_safe_violation_listList(){
                let item = {
					violationtime:undefined,
					violationcontent:undefined,
                }
				this.dataForm.mt_safe_violation_listList.push(item)
            },
            delmt_safe_violation_listList(index) {
                this.dataForm.mt_safe_violation_listList.splice(index, 1);
            },
			dataList(){
				var _data = JSON.parse(JSON.stringify(this.dataForm));
				_data.specialimage = JSON.stringify(_data.specialimage)
				for(let i=0;i<_data.mt_safe_accident_listList.length;i++){
				var _list = _data.mt_safe_accident_listList[i];
				}
				for(let i=0;i<_data.mt_safe_violation_listList.length;i++){
				var _list = _data.mt_safe_violation_listList[i];
				}
				return _data;
			},
			dataInfo(dataAll){
				let _dataAll =dataAll
				_dataAll.specialimage = JSON.parse(_dataAll.specialimage)
				for(let i=0;i<_dataAll.mt_safe_accident_listList.length;i++){
				var _list = _dataAll.mt_safe_accident_listList[i];
				}
				for(let i=0;i<_dataAll.mt_safe_violation_listList.length;i++){
				var _list = _dataAll.mt_safe_violation_listList[i];
				}
				this.dataForm=_dataAll
			},
        },
    }

</script>
